<?php

namespace app\common\trilateral;

use think\Db;

/**
 * Class Classify
 * @author  Joy
 * @email   133320113@qq.com
 * @package app\common\trilateral
 */
class Classify
{

    static public $treeList = [];


    // 前台展示
    public static function dimension($data, $pid = '0', $count = 1)
    {
        if (empty($data)) return false;

        if(!is_array($data)){
            $data = self::object_to_array($data);
        }
        foreach ($data as $key => $value) {

            if ($value['parentStr'] == '0,') {
                $value['topId'] = 0;
            } else {
                $ids = explode(',', $value['parentStr']);
                $value['topId'] = isset($ids['1']) ? $ids['1'] : '';
            }
            if ($value['parenTid'] == $pid) {
                $value['count'] = $count;

                self::$treeList [] = $value;
                unset($data[$key]);
                self::dimension($data, $value['id'], $count + 1);
            }
        }

        return self::$treeList;
    }


    // 获取parentStr
    public static function GetParentStr($parenTid, $table)
    {

        if ($parenTid == 0)
            $parentStr = '0,';
        else
            $parentStr = Db::name($table)->where('id', $parenTid)->value('parentStr') . $parenTid . ',';

        return $parentStr;
    }

    // count 添加获取
    public static function countAdd($parenTid, $table)
    {
        if ($parenTid == 0)
            $number = '1';
        else
            $number = (Db::name($table)->where('id', $parenTid)->value('count') + 1);

        return $number;
    }

    // count 编辑获取
    public static function countUpdate($id, $parenTid, $table)
    {
        if ($parenTid == 0)

            $number = '1';
        else
            $number = (Db::name($table)->where('id', $parenTid)->value('count') + 1);

        self::upCount($id, $number, $table);
        return $number;
    }

    // 修改str
    public static function UpParentStr($parenTid, $id, $repId, $table, $table2 = '')
    {
        if ($parenTid == 0)
            $parentStr = '0,';
        else
            $parentStr = Db::name($table)->where('id', $parenTid)->value('parentStr') . $parenTid . ',';

        //更新连级目录
        if ($parenTid != $id) {
            //更新所有关联parentStr
            if ($parenTid != $repId) {
                self::SonUpParenStr($id, $parentStr, $table, $table2);
            }
        }

        return $parentStr;
    }

    // 修改子集下的str
    protected static function SonUpParenStr($id, $parentStr, $table, $table2 = '', $pStr = '')
    {

        if ($pStr == '')
            $parStr = $parentStr . $id . ',';
        else
            $parStr = $pStr . $id . ',';

        //
        if ($table2) {
            Db::name($table2)->where('parenTid', $id)->update(['parentStr' => $parStr]);
        }
        //
        $data = Db::name($table)->where('parenTid', $id)->select();
        foreach ($data as $value) {
            //
            Db::name($table)->where('id', $value['id'])->update(['parentStr' => $parStr]);
            //
            if ($table2) {
                Db::name($table2)->where('parenTid', $value['id'])->update(['parentStr' => $parStr . $value['id'] . ',']);
            }
        }

    }

    // count 编辑ALL
    protected static function upCount($id, $number, $table)
    {

        $data = Db::name($table)->where('parenTid', $id)->select();

        foreach ($data as $value) {

            if ($number < $value['count'])
                $count = $value['count'] - ($value['count'] - ($number + 1));
            elseif ($number == $value['count'])
                $count = 1;
            else
                $count = $value['count'] + (($number + 1) - $value['count']);

            //执行更改
            Db::name($table)->where('id', $value['id'])->update(['count' => $count]);
            //继续
            self::upCount($value['id'], $value['count'], $table);
        }

    }

    /**
     * 对象 转 数组
     *
     * @param object $obj 对象
     * @return array
     */
    protected static function object_to_array($obj)
    {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)self::object_to_array($v);
            }
        }

        return $obj;
    }
}
